package work.droit.gui.swing;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.wb.swt.SWTResourceManager;

import work.droit.data.Definition;
import work.droit.gui.ApiController;
import org.eclipse.wb.swt.layout.grouplayout.GroupLayout;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.wb.swt.layout.grouplayout.LayoutStyle;

public class EditDefinitionsFrame extends Shell
{
	private Text	newDefinition;
	private String	textRecherche;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[])
	{
		try
		{
			Display display = Display.getDefault();
			EditDefinitionsFrame shell = new EditDefinitionsFrame(display);
			shell.open();
			shell.layout();
			while (!shell.isDisposed())
			{
				if (!display.readAndDispatch())
				{
					display.sleep();
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void launch()
	{
		Display display = getDisplay();
		this.open();
		this.layout();
		while (!this.isDisposed())
		{
			if (!display.readAndDispatch())
			{
				display.sleep();
			}
		}
	}

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public EditDefinitionsFrame(final Display display)
	{
		super(display, SWT.SHELL_TRIM);
		setBackground(ApiController.bgColor);
		setImage(SWTResourceManager.getImage(EditDefinitionsFrame.class, "/work/droit/rc/images/bubble_16.png"));

		Group grpNouvelleDfinitions = new Group(this, SWT.NONE);
		grpNouvelleDfinitions.setText("Nouvelle d\u00E9finitions");
		FontData fontData = grpNouvelleDfinitions.getFont().getFontData()[0];
		Font font = new Font(display, new FontData(fontData.getName(), fontData.getHeight(), SWT.ITALIC));
		grpNouvelleDfinitions.setFont(font);

		Group grpRechercheRapide = new Group(this, SWT.NONE);
		grpRechercheRapide.setText("Recherche rapide");
		fontData = grpRechercheRapide.getFont().getFontData()[0];
		font = new Font(display, new FontData(fontData.getName(), fontData.getHeight(), SWT.ITALIC));
		grpRechercheRapide.setFont(font);

		Group grpDfinitions = new Group(this, SWT.NONE);
		grpDfinitions.setText("D\u00E9finitions");
		fontData = grpDfinitions.getFont().getFontData()[0];
		font = new Font(display, new FontData(fontData.getName(), fontData.getHeight(), SWT.ITALIC));
		grpDfinitions.setFont(font);

		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout
				.setHorizontalGroup(groupLayout.createParallelGroup(GroupLayout.TRAILING).add(
						groupLayout
								.createSequentialGroup()
								.addContainerGap()
								.add(groupLayout
										.createParallelGroup(GroupLayout.TRAILING)
										.add(GroupLayout.LEADING, grpDfinitions, GroupLayout.DEFAULT_SIZE, 412,
												Short.MAX_VALUE)
										.add(GroupLayout.LEADING, grpRechercheRapide, GroupLayout.DEFAULT_SIZE, 412,
												Short.MAX_VALUE)
										.add(GroupLayout.LEADING, grpNouvelleDfinitions, GroupLayout.DEFAULT_SIZE, 412,
												Short.MAX_VALUE)).addContainerGap()));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(GroupLayout.LEADING).add(
				groupLayout.createSequentialGroup().addContainerGap()
						.add(grpNouvelleDfinitions, GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
						.addPreferredGap(LayoutStyle.RELATED)
						.add(grpRechercheRapide, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.RELATED)
						.add(grpDfinitions, GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE).addContainerGap()));
		grpDfinitions.setLayout(new FillLayout(SWT.HORIZONTAL));

		final Tree tree = new Tree(grpDfinitions, SWT.BORDER);
		FillLayout fl_grpRechercheRapide = new FillLayout(SWT.HORIZONTAL);
		fl_grpRechercheRapide.marginHeight = 10;
		grpRechercheRapide.setLayout(fl_grpRechercheRapide);

		final Text textRech = new Text(grpRechercheRapide, SWT.BORDER);
		textRech.setTextLimit(Definition.mot.size());
		FillLayout fl_grpNouvelleDfinitions = new FillLayout(SWT.HORIZONTAL);
		fl_grpNouvelleDfinitions.marginHeight = 10;
		grpNouvelleDfinitions.setLayout(fl_grpNouvelleDfinitions);
		textRech.addKeyListener(new KeyListener()
		{
			@Override
			public void keyReleased(KeyEvent e)
			{
			}

			@Override
			public void keyPressed(KeyEvent e)
			{
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR)
				{
					e.doit = false;
				}
				Text text = (Text) e.getSource();
				if (!text.getText().equals(textRecherche))
				{
					textRecherche = text.getText();
					ApiController.fillDefinitionTree(tree, textRecherche, false);
				}
			}
		});

		newDefinition = new Text(grpNouvelleDfinitions, SWT.BORDER);
		newDefinition.setTextLimit(Definition.mot.size());
		newDefinition.addKeyListener(new KeyListener()
		{
			@Override
			public void keyReleased(KeyEvent e)
			{
			}

			@Override
			public void keyPressed(KeyEvent e)
			{
				if (e.keyCode == SWT.CR || e.keyCode == SWT.KEYPAD_CR)
				{
					String text = newDefinition.getText();
					if (!"".equals(text))
					{
						Definition def = new Definition();
						def.setChamp(Definition.mot, text);
						def.setChamp(Definition.categorie, "");
						if (!def.exist(Definition.mot))
						{
							def.initialiseNewKey();
							def.insert();
							DefinitionFrame definitionFrame = new DefinitionFrame(display);
							definitionFrame.launch(def, true);
							ApiController.fillDefinitionTree(tree, textRech.getText(), false);
						}
					}
				}
			}
		});

		setLayout(groupLayout);

		MouseListener mouseListenerCat = new MouseListener()
		{
			@Override
			public void mouseUp(MouseEvent e)
			{
			}

			@Override
			public void mouseDown(MouseEvent e)
			{
			}

			@Override
			public void mouseDoubleClick(MouseEvent e)
			{
				Object o = e.getSource();
				Tree tree = (Tree) o;
				TreeItem[] ts = tree.getSelection();
				if (ts != null && ts.length == 1)
				{
					TreeItem t = ts[0];
					DefinitionFrame definitionFrame = new DefinitionFrame(display);
					definitionFrame.launch((Definition) t.getData(), true);
					ApiController.fillDefinitionTree(tree, textRech.getText(), false);
				}
			}
		};
		tree.addMouseListener(mouseListenerCat);

		ApiController.fillDefinitionTree(tree, "", false);

		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents()
	{
		setText("Logiciel droit - D\u00E9finitions");
		setSize(450, 536);

	}

	@Override
	protected void checkSubclass()
	{
		// Disable the check that prevents subclassing of SWT components
	}
}
